package com.goldplusgold.tradedelay.app.trade.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goldplusgold.support.lib.base.adapter.AdapterParentBase;
import com.goldplusgold.support.lib.widget.horizontallistview.SyncHorizontalScrollView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.trade.pojo.TriggeredRecord;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/2.
 */

public class AdapterTrigger extends AdapterParentBase<TriggeredRecord> implements SyncHorizontalScrollView.OnScrollListenter {
    private ArrayList<SyncHorizontalScrollView> mScrollViewList = new ArrayList<>();

    public AdapterTrigger(Context ctx) {
        super(ctx);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        //判断是否有缓存
        if (convertView == null) { //没有缓存
            //无缓存，则实例化布局
            convertView = getLayoutInflater().inflate(R.layout.item_triggred, parent, false);
            holder = new ViewHolder(convertView);

            for (int i = 0; i < 7; i++) {
                View childView = getLayoutInflater().inflate(R.layout.item_horizontal_position, holder.ll_item_triggered, false);
                holder.ll_item_triggered.addView(childView);
            }
            mScrollViewList.add(holder.mSyncHorizontalScrollView);
            holder.mSyncHorizontalScrollView.setOnScrollListenter(this);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        TriggeredRecord recordItem = getItem(position);

        if (recordItem != null) {
            // 合约代码
            final String prodCode = recordItem.getProdCode();
            holder.prodCode.setText(prodCode);

            int count = holder.ll_item_triggered.getChildCount();

            String bearbullType = recordItem.getBearbullType();
            String sltpType = recordItem.getSltpType();
            String sltpPrice = recordItem.getSltpPrice();
            String lots = recordItem.getLots();
            String commissionPrice = recordItem.getCommissionPrice();
            String triggerTime = recordItem.getTriggerTime();
            String state = recordItem.getState();

            for (int i = 0; i < count; i++) {
                View childView = holder.ll_item_triggered.getChildAt(i);
                TextView item = (TextView) childView.findViewById(R.id.id_text_view_horizontal);

                switch (i) {
                    case 0:
                        //多空
                        item.setText(bearbullType);
                        break;
                    case 1:
                        //类型
                        item.setText(sltpType);
                        break;
                    case 2:
                        //触发价
                        item.setText(sltpPrice);
                        break;
                    case 3:
                        // 手数
                        item.setText(lots);
                        break;
                    case 4:
                        //委托价
                        item.setText(commissionPrice);
                        break;
                    case 5:
                        //触发时间
                        item.setText(triggerTime);
                        break;
                    case 6:
                        //状态
                        item.setText(state);
                        break;
                }


            }

        }

        return convertView;
    }

    @Override
    public void onScroll(int x, int y) {
        for (SyncHorizontalScrollView scrollview : mScrollViewList) {
            scrollview.setScrollX(x);
        }
    }


    public final class ViewHolder {

        @BindView(R.id.tv_triggered_record_prod_code)
        TextView prodCode;

        @BindView(R.id.scroll_view_triggered)
        SyncHorizontalScrollView mSyncHorizontalScrollView;

        @BindView(R.id.ll_item_triggered)
        LinearLayout ll_item_triggered;

        public ViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }

    }
    public void addHead(SyncHorizontalScrollView view) {
        this.mScrollViewList.add(view);
    }

}
